package com.qsmx.store.app.main.mem;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.mem.adapter.MemCardManagerLostOrOpenAdapter;
import com.qsmx.store.app.main.mem.entity.MemCardLostOrOpenEntity;
import com.qsmx.store.app.main.mem.entity.MemManagerEntity;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import java.lang.reflect.Type;
import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class MemCardManagerLostOrOpen extends AtmosDelegate {

    @BindView(R2.id.et_person_num)
    AppCompatEditText etPersonNum;
    @BindView(R2.id.et_mem_num)
    AppCompatEditText etMemNum;

    @OnClick(R2.id.tv_search)
    void onSearch() {
        initMemData();
    }

    MemCardManagerLostOrOpenAdapter mAdapter;
    private MemCardLostOrOpenEntity mMemCardData;

    @BindView(R2.id.rlv_lost_card_list)
    RecyclerView rlvLostCardList;

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_manager_lost_or_open;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initRecycler();

    }

    private void initMemData() {

        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("deptId", String.valueOf(FustionManager.mMachineData.getDeptId()));
        params.put("memberNo", etMemNum.getText().toString());
        params.put("memberIdCard", etPersonNum.getText().toString());
        HttpHelper.RestClientGet(params, HttpUrl.MEMBER,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<MemCardLostOrOpenEntity>() {
                        }.getType();
                        Gson gson = new Gson();
                        mMemCardData = gson.fromJson(response, type);
                        mAdapter.setData(mMemCardData.getContent());
                        mAdapter.notifyDataSetChanged();
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        showShortToast(msg);
                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {
                        showShortToast("未知错误");
                    }
                });

    }

    private void setCardLossOrActive(String icNo, String operateType) {
        JSONObject params = new JSONObject();
        params.put("deptId", FustionManager.mMachineData.getDeptId());
        params.put("icNo", icNo);
        params.put("operateType", operateType);

        HttpHelper.RestClientPostRaw(params, HttpUrl.MEM_CARD_LOSS_AND_ACTIVE,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {
                        initMemData();
                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        showShortToast(msg);
                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {
                        showShortToast("未知错误");
                    }
                });
    }

    private void initRecycler() {

        mAdapter = new MemCardManagerLostOrOpenAdapter(getContext());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());

        rlvLostCardList.setLayoutManager(manager);
        rlvLostCardList.setAdapter(mAdapter);

        mAdapter.setonItemClickListener(new MemCardManagerLostOrOpenAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (mMemCardData.getContent().get(position).getIcCard().getIcStatus() == 1) {
                    setCardLossOrActive(mMemCardData.getContent().get(position).getIcCard().getIcNo(), "2");
                } else if (mMemCardData.getContent().get(position).getIcCard().getIcStatus() == 2) {
                    setCardLossOrActive(mMemCardData.getContent().get(position).getIcCard().getIcNo(), "1");
                }
            }
        });

    }
}
